﻿
Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService
Partial Class Control_Hinhdo_Create
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "jqueryEnable", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "jqueryEnable", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If
            getLoai()
            getNgonngu()
        End If
    End Sub
    Protected Function Save() As Boolean
        Using data As New VSEntities
            Dim p As New VSModel.HinhDo
            Try
                p.HinhDoCode = txtHinhdocode.Text.Trim()
                p.TenGoi = txtTengoi.Text.Trim()
                p.DacTinh = txtDactinh.Text.Trim()
                p.LoaiId = ddlLoai.SelectedValue
                p.NgonNguId = ddlNgonngu.SelectedValue
                p.FileURL = hidImagePath.Text
                p.FileURLNone = hidImagePathNone.Text
                data.HinhDoes.AddObject(p)
                data.SaveChanges()
                Insert_App_Log("Insert  Hinhdo:" & txtHinhdocode.Text.Trim & "", Function_Name.HinhDo, Audit_Type.Create, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                Return True
            Catch ex As Exception
                log4net.Config.XmlConfigurator.Configure()
                log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                Return False
            End Try
        End Using
    End Function
    Protected Sub getNgonngu()
        ddlNgonngu.Items.Clear()
        ddlNgonngu.AppendDataBoundItems = True
        ddlNgonngu.Items.Add(New ListItem("---Chọn---", ""))
        Using data As New VSEntities
            Dim p = (From q In data.Languages Select q.LanguageId, q.Name).ToList
            ddlNgonngu.DataValueField = "LanguageId"
            ddlNgonngu.DataTextField = "Name"
            ddlNgonngu.DataSource = p
            ddlNgonngu.DataBind()
            ddlNgonngu.SelectedValue = 0
        End Using
    End Sub
    Protected Sub getLoai()
        ddlLoai.Items.Clear()
        ddlLoai.AppendDataBoundItems = True
        ddlLoai.Items.Add(New ListItem("---Chọn---", ""))
        Using data As New VSEntities
            Dim p = (From q In data.HinhDoTypes Select q.TypeId, q.Name).ToList
            ddlLoai.DataValueField = "TypeId"
            ddlLoai.DataTextField = "Name"
            ddlLoai.DataSource = p
            ddlLoai.DataBind()
            ddlLoai.SelectedValue = 0
        End Using
    End Sub
    Protected Sub ResetControl()
        txtHinhdocode.Text = ""
        txtTengoi.Text = ""
        txtDactinh.Text = ""
        ddlLoai.SelectedIndex = -1
        ddlNgonngu.SelectedIndex = -1
        hidImagePath.Text = ""
        imgUpload.ImageUrl = MainDomain & "Images/NoPhotoAvailable.jpg"

    End Sub
#End Region
#Region "Event for control "
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If Save() Then
            Excute_Javascript("Alertbox('Cập nhật dữ liệu thành công.');window.location ='../../Page/Hinhdo/List.aspx';", Me.Page, True)
        Else
            Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
        End If
    End Sub
    Protected Sub btnSaveAndNew_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSaveAndNew.Click
        If Save() Then
            Excute_Javascript("Alertbox('Cập nhật dữ liệu thành công.');", Me.Page, True)
            ResetControl()
        Else
            Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
        End If
    End Sub
    Protected Sub btnReset_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReset.Click
        ResetControl()
    End Sub
    Protected Sub btnBack_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnBack.Click
        Response.Redirect("List.aspx")
    End Sub
#End Region

    'Public Sub UploadData()
    '    If flDocument.HasFile Then
    '        ''Checkfile extension
    '        Dim fileExtension As String = If(flDocument.FileName.IndexOf(".") <> -1, flDocument.FileName.Split(".")(1), "")
    '        If Not (fileExtension = "PDF" Or fileExtension = "pdf") Then
    '            Excute_Javascript("Alertbox('Vui lòng chọn đúng loại file upload .pdf');", Me.Page, True)
    '            Exit Sub
    '        End If

    '        'Upload file len server, vi khi dung fireFox Or Chrom ko lay duoc duong dan thuc su cua file.
    '        txtFileurl.Text = Server.MapPath("~") & "\Uploads\" & flDocument.FileName
    '        Try
    '            'neu ton tai file --> xoa
    '            If System.IO.File.Exists(txtFileName.Text) = True Then
    '                System.IO.File.Delete(txtFileName.Text)
    '            End If
    '            'upload file lên Server
    '            flDocument.SaveAs(txtFileName.Text)

    '        Catch ex As Exception
    '            lblSuccess.Text = ex.Message.ToString
    '            Excute_Javascript("ShowDiv(0);", Me.Page, True)
    '            Exit Sub
    '        Finally
    '        End Try
    '    Else
    '        Excute_Javascript("Alertbox('Vui lòng chọn file dữ liệu ');", Me.Page, True)
    '    End If
    'End Sub

End Class
